package com.face.ali_face_plugin;

import android.app.Activity;
import android.content.Context;

import com.alipay.face.api.ZIMCallback;
import com.alipay.face.api.ZIMFacade;
import com.alipay.face.api.ZIMFacadeBuilder;
import com.alipay.face.api.ZIMResponse;

import java.util.HashMap;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;

/** AliFacePlugin */
public class AliFacePlugin implements FlutterPlugin, MethodCallHandler, ActivityAware {
  /// The MethodChannel that will the communication between Flutter and native Android
  ///
  /// This local reference serves to register the plugin with the Flutter Engine and unregister it
  /// when the Flutter Engine is detached from the Activity
  private MethodChannel channel;
  private Context context;
  private Activity activity;

  @Override
  public void onAttachedToEngine(FlutterPluginBinding flutterPluginBinding) {
    channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "com.cgwallet.app.channel/face");
    channel.setMethodCallHandler(this);
    context = flutterPluginBinding.getApplicationContext();
  }

  @Override
  public void onMethodCall(MethodCall call, Result result) {
    if (call.method.equals("getPlatformVersion")) {
      result.success("Android " + android.os.Build.VERSION.RELEASE);
    } else if(call.method.equals("faceInit")){
      ZIMFacade.install(context);
    }else if(call.method.equals("getMetainfo")) {
      String metaInfos = ZIMFacade.getMetaInfos(context);
      result.success(metaInfos);
    }else if(call.method.equals("startVerify")){
      String token = call.argument("token");
      ZIMFacade zimFacade = ZIMFacadeBuilder.create(activity);
      HashMap<String, String> extParams = new HashMap<>();
      extParams.put(ZIMFacade.ZIM_EXT_PARAMS_KEY_USE_VIDEO, ZIMFacade.ZIM_EXT_PARAMS_VAL_USE_VIDEO_TRUE);
      extParams.put(ZIMFacade.ZIM_EXT_PARAMS_KEY_FACE_PROGRESS_COLOR, "#FF0000");
      zimFacade.verify(token, true, extParams, new ZIMCallback() {
        @Override
        public boolean response(final ZIMResponse response) {
          int code = response.code;
          String title = "刷脸成功";
          if (1000 == code) {
            title = "刷脸成功";
          } else{
            title = "刷脸失败 ERRORCODE=" + code + "  reason=" + response.reason;
          }
          HashMap<String, String> dataDic = new HashMap<>();
          dataDic.put("title",title);
          dataDic.put("code",String.valueOf(code));
          dataDic.put("error",response.reason);
//                    dataDic.put("image",response.cardImageContent.toString());
          result.success(dataDic);
          return true;
        }
      });
    }
  }

  @Override
  public void onDetachedFromEngine(FlutterPluginBinding binding) {
    channel.setMethodCallHandler(null);
  }

  @Override
  public void onAttachedToActivity(ActivityPluginBinding activityPluginBinding) {
    activity = activityPluginBinding.getActivity();
  }

  @Override
  public void onDetachedFromActivityForConfigChanges() {

  }

  @Override
  public void onReattachedToActivityForConfigChanges(ActivityPluginBinding activityPluginBinding) {

  }

  @Override
  public void onDetachedFromActivity() {
    activity = null;
  }
}
